<?php
//*******************************************************
//* JAccelerator Plugin
//* http://fun.kubera.org
//* (C) Radoslaw Kubera
//* license http://www.gnu.org/copyleft/gpl.html GNU/GPL
//*******************************************************

defined( '_VALID_MOS' ) or die();

if (isset($_MAMBOTS)){
	$_MAMBOTS->registerFunction( 'onStart', 'JoomlaCacheStart' );
	$_MAMBOTS->registerFunction( 'onAfterStart', 'JoomlaCacheAfterStart' );
}

global $mosConfig_absolute_path,$mosConfig_lang;

if (file_exists($mosConfig_absolute_path."/administrator/components/com_joomlacache/language/$mosConfig_lang.php")) {
	include_once ($mosConfig_absolute_path."/administrator/components/com_joomlacache/language/$mosConfig_lang.php");
}else{
	include_once ($mosConfig_absolute_path."/administrator/components/com_joomlacache/language/english.php");
}


function JoomlaCacheGetMicrotime(){
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec);
}

function JoomlaCacheShowTimestamp($time){
	global $_MOS_OPTION;
	if (isset($_MOS_OPTION['buffer'])){
		$string = '<div id="timestamp" align="center" style="font-size:9px;">'._JOOMLACACHE_GENERATED_TIME.' '.$time.' '._JOOMLACACHE_GENERATION_SECONDS.'</div>';
		$_MOS_OPTION['buffer'] = preg_replace("/<div id=\"timestamp\".*<\/div>/",$string,$_MOS_OPTION['buffer']);
	}
}

function JoomlaCacheStart(){
	$_SERVER['joomlacachestart']=JoomlaCacheGetMicrotime();
	global $mosConfig_absolute_path;
	$_SERVER['joomlacacheabs'] = $mosConfig_absolute_path;
	$path = getcwd();
	$mosConfig_absolute_path = $path.'/components/com_joomlacache';
	chdir ($mosConfig_absolute_path);
}

if (file_exists($mosConfig_absolute_path.'/components/com_joomlacache/joomlacache.php')){
	require_once $mosConfig_absolute_path.'/components/com_joomlacache/joomlacache.php';
}else{
	die('error-main-file-joomlacache');
}

function JoomlaCacheAfterStart(){
	global $mosConfig_absolute_path,$mosConfig_lang;
	if (!file_exists($mosConfig_absolute_path.'/components/com_joomlacache/joomlacache.php')) return;

	global $mainframe,$mosConfig_absolute_path,$mosConfig_cachetime,$option,$mosConfig_gzip;

	JoomlaCacheCheckFlushRemove();

	$path_cache = $mosConfig_absolute_path.'/components/com_joomlacache/cache/';

	if (!is_dir($path_cache)) @mkdir ($path_cache);

	$params['default_cache_time']=$mosConfig_cachetime;
	$path_config =  $mosConfig_absolute_path.'/components/com_joomlacache/config.php';
	if (file_exists($path_config)){
		include $path_config;
	}

	$time = $params['default_cache_time'];

	if (isset ($params['size']) && $params['size']){
		global $database;
		$database->setQuery("select sum(size) from #__joomlacache");
		$size = $database->loadResult();
		if ($size>$params['size']){
			$_SERVER['joomlacachelimit']='limit';
		}
		$_SERVER['joomlacachesize']=$size;
	}

	 $type=0;
	if (!JoomlaCacheBaypass($time,$type)){
		$my = $mainframe->getUser();
		$cachetime=$time;
		$url = $_SERVER['REQUEST_URI'];
		$url = $mosConfig_lang.JoomlaCacheConvertLink($url);
		$path_cache             = $mosConfig_absolute_path.'/components/com_joomlacache/cache/';
		$path_components        = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/';
		$path_components_option = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$option.'/';
		$path_components_files = $mosConfig_absolute_path.'/components/com_joomlacache/cache/components/'.$option.'/files/';

		if (!$my->id && $type==1){
			@mkdir ($path_cache);
			@mkdir ($path_components);
			@mkdir ($path_components_option);
			@mkdir ($path_components_files);
			$_SERVER['cache_path']=$mainframe->_path->front;
			$mainframe->_path->front = $mosConfig_absolute_path.'/components/com_joomlacache/mainbody.php';
		}else{
			if (file_exists($path_components_files.$url)){
				@unlink ($path_components_files.$url);
				global $database;
				$database->setQuery("delete from #__joomlacache where filename='$url' and component='1'");
				$database->query();
			}
		}
	}

	if ($type==0){
		$_SERVER['cache_path']=$mainframe->_path->front;
		$mainframe->_path->front = $mosConfig_absolute_path.'/components/com_joomlacache/mainbody.php';
	}

	if ($option=="com_joomlacache"){
		$_SERVER['cache_path']=$mainframe->_path->front;
		$mainframe->_path->front = $mosConfig_absolute_path.'/components/com_joomlacache/mainbody.php';
	}

}

?>
